package com.maiyuren.fishkit.utils
typealias FishEventBusCallback = (param: Any?)->Unit

enum class FishEvent {
    goWord,
    searchResult,
    clearFavourite,
}

object FishEventBus {
    private val cbs: MutableMap<FishEvent, MutableList<FishEventBusCallback>> = mutableMapOf()
    fun emit(event: FishEvent, param: Any?) {
        val cbList = cbs[event]
        cbList?.forEach {
            it(param)
        }
    }

    fun on(event: FishEvent, cb: FishEventBusCallback) {
        var cbList = cbs[event]
        if (cbList == null) {
            cbList = mutableListOf()
            cbs[event] = cbList
        }
        cbList.add(cb)
    }

    fun off(event: FishEvent, cb: FishEventBusCallback) {
        var cbList = cbs[event]
        if (cbList != null) {
            cbList.remove(cb)
        }
    }
}

